package com.gewu.pm.ui.fragment.message;

import android.widget.TextView;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.gewu.pm.R;
import com.gewu.pm.aop.SingleClick;
import com.gewu.pm.app.AppActivity;
import com.gewu.pm.app.AppFragment;
import com.gewu.pm.bean.TestBean;
import com.gewu.pm.ui.adapter.PendingWorkAdapter;
import com.gewu.pm.ui.popup.SelectTimePopup;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * 待办工作 Fragment
 */
public final class PendingWorkFragment extends AppFragment<AppActivity>
        implements PendingWorkAdapter.OnButtonClickListener {

    private TextView mTypeView;
    private RecyclerView mRecyclerView;
    private PendingWorkAdapter mAdapter;
    private SelectTimePopup.Builder mTypePopup;

    private int mCurrentFilterType = 0; // 当前筛选类型：0=全部，1=促销活动审批，2=打卡流程审批
    private List<String> mTypeList = Arrays.asList("全部", "促销活动审批", "打卡流程审批");
    private List<TestBean> mAllDataList = new ArrayList<>(); // 全部数据

    public static PendingWorkFragment newInstance() {
        return new PendingWorkFragment();
    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_pending_work;
    }

    @Override
    protected void initView() {
        mTypeView = findViewById(R.id.tv_type);
        mRecyclerView = findViewById(R.id.rv_list);

        // 设置类型选择器点击
        setOnClickListener(mTypeView);

        // 设置列表
        mRecyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
        mAdapter = new PendingWorkAdapter(getContext());
        mAdapter.setOnButtonClickListener(this);
        mRecyclerView.setAdapter(mAdapter);
    }

    @Override
    protected void initData() {
        loadData();
    }

    /**
     * 加载数据
     */
    private void loadData() {
        mAllDataList.clear();
        
        // 创建测试数据：type = "1" → 促销活动审批
        mAllDataList.add(new TestBean("1", "购买xxx商品1箱，赠送xxx商品1盒", "1"));
        mAllDataList.add(new TestBean("2", "购买金额达60元送1盒内酯豆腐", "1"));
        
        // 创建测试数据：type = "2" → 打卡流程审批
        mAllDataList.add(new TestBean("3", "李四的补卡申请需要审批", "2"));
        mAllDataList.add(new TestBean("4", "李四的补卡申请需要审批", "2"));
        
        // 初始显示全部数据
        filterData();
    }

    /**
     * 根据当前筛选类型过滤数据
     */
    private void filterData() {
        List<TestBean> filteredList = new ArrayList<>();
        
        if (mCurrentFilterType == 0) {
            // 全部：显示所有数据
            filteredList.addAll(mAllDataList);
        } else {
            // 按 type 筛选
            String typeStr = String.valueOf(mCurrentFilterType);
            for (TestBean bean : mAllDataList) {
                if (typeStr.equals(bean.getType())) {
                    filteredList.add(bean);
                }
            }
        }
        
        mAdapter.setData(filteredList);
    }

    @SingleClick
    @Override
    public void onClick(android.view.View view) {
        int viewId = view.getId();
        if (viewId == R.id.tv_type) {
            // 显示类型选择弹窗
            showTypePopup();
        }
    }

    /**
     * 显示类型选择弹窗
     */
    private void showTypePopup() {
        if (mTypePopup == null) {
            mTypePopup = new SelectTimePopup.Builder(getContext())
                    .setList(mTypeList)
                    .setSelectedPosition(mCurrentFilterType)
                    .setListener((popup, data) -> {
                        // 根据选中的文本找到对应的位置
                        int position = mTypeList.indexOf(data);
                        if (position != -1) {
                            mCurrentFilterType = position;
                            mTypeView.setText(data);
                            // 重新过滤数据
                            filterData();
                        }
                    });
        } else {
            mTypePopup.setSelectedPosition(mCurrentFilterType);
        }
        mTypePopup.showAsDropDown(mTypeView);
    }

    @Override
    public void onRejectToInitiator(int position, TestBean item) {
        toast("驳回发起人：" + item.getName());
        // TODO: 实现驳回发起人逻辑
    }

    @Override
    public void onRejectToPrevious(int position, TestBean item) {
        toast("驳回上一级：" + item.getName());
        // TODO: 实现驳回上一级逻辑
    }

    @Override
    public void onApprove(int position, TestBean item) {
        toast("审核通过：" + item.getName());
        // TODO: 实现审核通过逻辑
    }
}

